package com.bootdo.lvyou_specialty.service.impl;

import com.bootdo.lvyou_specialty.dao.LvyouSpecialtyProductDao;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.bootdo.lvyou_specialty.dao.LvyouSpecialtyOrderContentDao;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyOrderContent;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyOrderContentService;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.transform.Result;


@Service
public class LvyouSpecialtyOrderContentServiceImpl implements LvyouSpecialtyOrderContentService {
	@Autowired
	private LvyouSpecialtyOrderContentDao lvyouSpecialtyOrderContentDao;

	@Autowired
	private LvyouSpecialtyProductDao lvyouSpecialtyProductDao;
	
	@Override
	public LvyouSpecialtyOrderContent get(Long id){
		return lvyouSpecialtyOrderContentDao.get(id);
	}
	
	@Override
	public List<LvyouSpecialtyOrderContent> list(Map<String, Object> map){
		return lvyouSpecialtyOrderContentDao.list(map);
	}

	@Override
	public List<String> listGroup(Map<String, Object> map) {
		return lvyouSpecialtyOrderContentDao.listGroup(map);
	}

	@Override
	public List<LvyouSpecialtyOrderContent> listGroupList(List<String> list) {
		return lvyouSpecialtyOrderContentDao.listGroupList(list);
	}

	@Override
	public int count(Map<String, Object> map){
		return lvyouSpecialtyOrderContentDao.count(map);
	}

	@Override
	public List<Integer> merchantCount(Map<String, Object> map) {
		return lvyouSpecialtyOrderContentDao.merchantCount(map);
	}

	@Override
	public int save(LvyouSpecialtyOrderContent lvyouSpecialtyOrderContent){
	
		if(lvyouSpecialtyOrderContent.getId() != null) {
			return lvyouSpecialtyOrderContentDao.update(lvyouSpecialtyOrderContent);
		}else {
			return lvyouSpecialtyOrderContentDao.save(lvyouSpecialtyOrderContent);
		}		
	
		
	}
	
	@Override
	public int update(LvyouSpecialtyOrderContent lvyouSpecialtyOrderContent){
		return lvyouSpecialtyOrderContentDao.update(lvyouSpecialtyOrderContent);
	}
	
	@Override
	public int remove(Long id){
		return lvyouSpecialtyOrderContentDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return lvyouSpecialtyOrderContentDao.batchRemove(ids);
	}

	@Override
	public List<LvyouSpecialtyOrderContent> getByOrderNo(String orderNo) {
		return lvyouSpecialtyOrderContentDao.getByOrderNo(orderNo);
	}

	@Override
	public List<LvyouSpecialtyOrderContent> getByMerchantOrderNo(String merchantOrderNo) {
		return lvyouSpecialtyOrderContentDao.getByMerchantOrderNo(merchantOrderNo);
	}

	@Override
	public List<LvyouSpecialtyOrderContent> getByTkOrderNo(String tkOrderNo) {
		return lvyouSpecialtyOrderContentDao.getByTkOrderNo(tkOrderNo);
	}

	@Override
	public List<LvyouSpecialtyOrderContent> getByOrderNoAndMerchant(String orderNo, Long merchantId) {
		return lvyouSpecialtyOrderContentDao.getByOrderNoAndMerchant(orderNo, merchantId);
	}

	@Override
	public int updateStatus(String orderNo, String wxOrderNo) {
		return lvyouSpecialtyOrderContentDao.updateStatus(orderNo, wxOrderNo);
	}

	@Override
	public int editDelivery(String orderNo, String logisticType, String logisticNumber) {
		return lvyouSpecialtyOrderContentDao.editDelivery(orderNo, logisticType, logisticNumber);
	}

	@Override
	public int changeStatus(String merchantOrder, Integer status) {
		return lvyouSpecialtyOrderContentDao.changeStatus(merchantOrder, status);
	}

	@Override
	public int tkPre(String merchantOrderNo, String tkOrderNo) {
		return lvyouSpecialtyOrderContentDao.tkPre(merchantOrderNo, tkOrderNo);
	}

	@Override
	public int tkWxCallBack(String tkOrderNo, String tkWxOrderNo, Integer status) {
		return lvyouSpecialtyOrderContentDao.tkWxCallBack(tkOrderNo, tkWxOrderNo, status);
	}

	@Override
	public int tkOrderNo(String merchantOrderNo, String tkOrderNo, Integer status) {
		return lvyouSpecialtyOrderContentDao.tkOrderNo(merchantOrderNo, tkOrderNo, status);
	}

	@Override
	public int createTuikuan(String merchantOrder, Integer status) {
		return 0;
	}

	@Transactional
	@Override
	public int tkBackChangeNumber(String tkOrderNo, String tkWxOrderNo, Integer status) throws NoSuchMethodError{
		tkWxCallBack(tkOrderNo, tkWxOrderNo, status);
		List<LvyouSpecialtyOrderContent> contentSqlList = lvyouSpecialtyOrderContentDao.getByTkOrderNo(tkOrderNo);
		int i = changNumber(contentSqlList);
		return i;
	}

	private int changNumber(List<LvyouSpecialtyOrderContent> contentSqlList) {
		int result = 0;

		System.err.println(contentSqlList);
		for (LvyouSpecialtyOrderContent content : contentSqlList) {
			// 做销量的减少处理
			Long goodsId = content.getGoodsId();
			Integer goodsNumber = content.getGoodsNumber();
			LvyouSpecialtyProduct lvyouSpecialtyProduct = lvyouSpecialtyProductDao.get(goodsId);
			lvyouSpecialtyProduct.setSellNumber(lvyouSpecialtyProduct.getSellNumber() - goodsNumber);

			// 做库存的增加处理
			lvyouSpecialtyProduct.setReportory(lvyouSpecialtyProduct.getReportory() + goodsNumber);
			if (lvyouSpecialtyProduct.getReportory() < 0) {
				throw new NoSuchMethodError();
			}

			result = lvyouSpecialtyProductDao.update(lvyouSpecialtyProduct);
			System.err.println("完成了");

			if (result <= 0) {
				throw new NoSuchMethodError();
			}
		}
		return result;
	}
}
